
package 
{
	
	import flare.display.TextSprite;
	import flare.vis.Visualization;
	import flare.vis.controls.DragControl;
	import flare.vis.data.Data;
	import flare.vis.data.EdgeSprite;
	import flare.vis.data.NodeSprite;
	import flare.vis.operator.label.Labeler;
	import flare.vis.operator.layout.CircleLayout;
	
	import flash.display.Sprite;
	import flash.filters.DropShadowFilter;
	import flash.geom.Rectangle;
	import flash.text.TextFormat;
	
	/**
	 * Simple demo using the Labeler operator exclusively to display a node (NodeSprite.shape = null)
	 * 
	 * @author <a href="http://goosebumps4all.net">martin dudek</a>
	 */
	
	[SWF(width="500", height="500", backgroundColor="#ffff00", frameRate="30")]
	
	public class LabelerBorderDemo extends Sprite
	{
		
		private var vis:Visualization;
		
		private var info:TextSprite;
		
		public function LabelerBorderDemo() {
			
			var w:Number = stage.stageWidth;
			var h:Number = stage.stageHeight;
				
			var data:Data = honeycomb(1);
			
			vis = new Visualization(data);
			vis.bounds = new Rectangle(10, 10, w-20, h-20);
			
			vis.data.nodes.setProperties({
				"shape"      : null, //no shape
				"buttonMode" : true,		
				"filters"    : [new DropShadowFilter(7)]	
			});
			
			vis.data.edges.setProperties({
				"lineWidth" : 4,
				"lineColor" : 0xffff0000
			});
	
			
			var i:int = 0;
			vis.data.nodes.visit(function(ns:NodeSprite):void {
				ns.data.label = "Node " + i++;
			});
			
			i=0;
			vis.data.edges.visit(function(es:EdgeSprite):void {
				es.data.label = "Edge " + i++;
			});
			
			// controls
			
			var dc:DragControl = new DragControl(NodeSprite);
			vis.controls.add(dc);
			
			// operators
			
			var lay:CircleLayout = new CircleLayout();
			vis.operators.add(lay);
			
			var fmt:TextFormat = new TextFormat();
			fmt.font = "Arial";
			fmt.color = 0xdddddd;
			fmt.size = 21;
			fmt.bold = true;
	
			
			var lan:Labeler = new Labeler("data.label",Data.NODES,fmt,NodeSprite);
			vis.operators.add(lan);
			
			lan.operate(); // so that the props.label fields are set
			
			vis.data.nodes.setProperties({
				"props.label.textField.border": true,
				"props.label.textField.borderColor": 0xff0000,
				"props.label.textField.background": true,
				"props.label.textField.backgroundColor": 0x0000ff
			});
			
			vis.update();
			
			addChild(vis);
			
		}
		
		
		/**
		* from flare.demos.util.GraphUtil
		*/
		private function honeycomb(levels:uint):Data
	    {
	        var g:Data = new Data();
	        var layer1:Array = halfcomb(g, levels);
	        var layer2:Array = halfcomb(g, levels);
	        for (var i:uint=0; i<(levels<<1); ++i) {
	            var n1:NodeSprite = layer1[i];
	            var n2:NodeSprite = layer2[i];
	            g.addEdgeFor(n1, n2);
	        }
	        return g;
	    }
	       private static function halfcomb(g:Data, levels:uint):Array
	    {
	    	var top:Array = new Array();
	    	var layer:Array = new Array();
	        var label:uint = 0, i:uint, j:uint;
	        
	        for (i=0; i<levels; ++i) {
	            var n:NodeSprite = g.addNode();
	            n.data.label = String(label++);
	            top.push(n);
	        }
	        for (i=0; i<levels; ++i) {
	            n = null;
	            for (j=0; j<top.length; ++j) {
	                var p:NodeSprite = top[j];
	                if (n == null) {
	                    n = g.addNode();
	                    n.data.label = String(label++);
	                    layer.push(n);
	                }
	                g.addEdgeFor(p, n);
	                n = g.addNode();
	                n.data.label = String(label++);
	                layer.push(n);
	                g.addEdgeFor(p, n);
	            }
	            if (i == levels-1) {
	                return layer;
	            }
	            top.splice(0, top.length);
	            for (j=0; j<layer.length; ++j) {
	                p = layer[j];
	                n = g.addNode();
	                n.data.label = String(label++);
	                top.push(n);
	                g.addEdgeFor(p, n);
	            }
	            layer.splice(0, layer.length);
	        }
	        // should never happen
	        return top;
	    }
	}
}	

